﻿using System;
using System.Collections.Generic;
using System.Text;

using ChemicalEquilibrium;

namespace NeuralModule
{
    /// <summary>
    /// 感受器
    /// 分类:
    ///     简单感受器
    ///         功能:
    ///             将能量转换为离子通道的开放
    ///         组成:
    ///             
    ///     视觉感受器
    ///     听觉感受器
    ///     嗅觉感受器
    ///     味觉感受器
    ///     痛觉感受器
    ///     压力感受器
    ///     温度感受器
    ///     
    /// </summary>
    public class Receptor
    {
        public Reactants IonK, IonNa;
        public Reactants ChannelK, ChannelNa, PumpNaK;

        public Reactants Energy;
        public Reactants ReceptorEnergyChannelNa;

        public List<ThreadEventWait> TEWs = new List<ThreadEventWait>();

        public IonicEquilibrium IEK, IENa, IEEnergyNa;

        public IonicPump IPNaK;

        public EnergyTransform ETChannelNa;

        public ReactActivate RAReceptorEnergy;

        public Receptor()
        {
            Initialize();
        }

        public void Initialize()
        {
            //离子
            this.IonK = new Reactants(100000, 100000);
            this.IonNa = new Reactants(100000, 100000);

            //离子通道
            this.ChannelK = new Reactants(1000, 0);
            this.ChannelNa = new Reactants(1000, 0);
            
            //离子泵
            this.PumpNaK = new Reactants(20, 0);

            //能量
            this.Energy = new Reactants(0, 1000);

            //能量受体Na离子通道
            this.ReceptorEnergyChannelNa = new Reactants(0, 1000);

            
            //离子平衡系统
            this.IEK = new IonicEquilibrium(this.ChannelK.Active, this.IonK.Active, this.IonK.Inactive, 0.009f, -80, this.GetVoltage);
            this.IENa = new IonicEquilibrium(this.ChannelNa.Active, this.IonNa.Active, this.IonNa.Inactive, 0.001f, 62, this.GetVoltage);

            //离子泵平衡系统
            this.IPNaK = new IonicPump(this.PumpNaK.Active, new Queue<Reactant>[] { this.IonK.Active, this.IonNa.Active }, new Queue<Reactant>[] { this.IonK.Inactive, this.IonNa.Inactive }, new int[] { 200, -200 }, new int[] { 90000, 110000 });

            //感受器离子通道
            this.IEEnergyNa = new IonicEquilibrium(this.ReceptorEnergyChannelNa.Active, this.IonNa.Active, this.IonNa.Inactive, 0.2f, 62, this.GetVoltage);

            //感受器失活
            this.RAReceptorEnergy = new ReactActivate(this.ReceptorEnergyChannelNa.Active, this.ReceptorEnergyChannelNa.Inactive, null, null, 10, 10, 1, 1);

            //能量与受体转换
            this.ETChannelNa = new EnergyTransform(this.Energy.Active, this.Energy.Inactive, this.ReceptorEnergyChannelNa.Active, this.ReceptorEnergyChannelNa.Inactive);


            //离子平衡线程
            this.TEWs.Add(new ThreadEventWait(10, new OperateCallback(this.IEK.Transport)));
            this.TEWs.Add(new ThreadEventWait(10, new OperateCallback(this.IENa.Transport)));
            this.TEWs.Add(new ThreadEventWait(10, new OperateCallback(this.IPNaK.Transport)));

            this.TEWs.Add(new ThreadEventWait(10, new OperateCallback(this.IEEnergyNa.Transport)));

            this.TEWs.Add(new ThreadEventWait(10, new OperateCallback(this.RAReceptorEnergy.Inactivate)));

            //能量转换线程
            this.TEWs.Add(new ThreadEventWait(10, new OperateCallback(this.ETChannelNa.Activate)));

            
        }

        public void Start()
        {
            foreach (ThreadEventWait TEW in this.TEWs) TEW.Start();
        }

        public void Abort()
        {
            foreach (ThreadEventWait TEW in this.TEWs) TEW.Abort();
        }

        float Capacitance = 0.0005f;
        public int GetVoltage()
        {
            int voltage = (int)((this.IonK.Active.Count + this.IonNa.Active.Count - this.IonK.Inactive.Count - this.IonK.Inactive.Count) * this.Capacitance);
            return voltage;
        }
    }


}
